/**
 * created by dt 2025-05-26
 * 属性系统演示
 */

using UnityEngine;
using DLib.Attribute;

namespace DLib.Attribute.Demo
{
    /// <summary>
    /// 属性ID常量
    /// </summary>
    public static class AttributeIds
    {
        public const int Attack = 1;
        public const int Defense = 2;
        public const int Health = 3;
        public const int Speed = 4;
        public const int CriticalRate = 5;
        public const int CriticalDamage = 6;
    }

    /// <summary>
    /// 属性系统演示组件
    /// </summary>
    public class AttributeDemo : MonoBehaviour
    {
        [Header("基础属性")]
        public int baseAttack = 100;
        public int baseDefense = 50;
        public int baseHealth = 1000;
        public int baseSpeed = 10;

        [Header("装备加成")]
        public float weaponAttackBonus = 0.2f; // 20%攻击加成
        public int armorDefenseBonus = 30;     // 30点防御加成

        [Header("Buff效果")]
        public float berserkAttackBonus = 0.5f; // 狂暴50%攻击加成
        public float shieldDefenseBonus = 0.3f; // 护盾30%防御加成

        private Attributes attributes;
        private AttributeModifier weaponModifier;
        private AttributeModifier armorModifier;
        private AttributeModifier berserkModifier;
        private AttributeModifier shieldModifier;

        void Start()
        {
            InitializeAttributes();
            SetupEquipment();
            SetupBuffs();
            
            // 监听属性变化
            attributes.OnAttributeChanged += OnAttributeChanged;
            
            // 打印初始属性
            PrintAttributes();
        }

        void InitializeAttributes()
        {
            attributes = new Attributes();
            
            // 设置基础属性
            attributes.SetBaseValue(AttributeIds.Attack, baseAttack);
            attributes.SetBaseValue(AttributeIds.Defense, baseDefense);
            attributes.SetBaseValue(AttributeIds.Health, baseHealth);
            attributes.SetBaseValue(AttributeIds.Speed, baseSpeed);
        }

        void SetupEquipment()
        {
            // 武器：百分比攻击加成
            weaponModifier = attributes.AddPercentageModifier(
                AttributeIds.Attack, 
                weaponAttackBonus, 
                ModifierPriority.Equipment
            );

            // 护甲：绝对值防御加成
            armorModifier = attributes.AddFlatModifier(
                AttributeIds.Defense, 
                armorDefenseBonus, 
                ModifierPriority.Equipment
            );
        }

        void SetupBuffs()
        {
            // 狂暴：百分比攻击加成
            berserkModifier = attributes.AddPercentageModifier(
                AttributeIds.Attack, 
                berserkAttackBonus, 
                ModifierPriority.Buff
            );

            // 护盾：百分比防御加成
            shieldModifier = attributes.AddPercentageModifier(
                AttributeIds.Defense, 
                shieldDefenseBonus, 
                ModifierPriority.Buff
            );
        }

        void OnAttributeChanged(int attributeId, int oldValue, int newValue)
        {
            string attributeName = GetAttributeName(attributeId);
            Debug.Log($"{attributeName}: {oldValue} -> {newValue}");
        }

        string GetAttributeName(int attributeId)
        {
            switch (attributeId)
            {
                case AttributeIds.Attack: return "攻击力";
                case AttributeIds.Defense: return "防御力";
                case AttributeIds.Health: return "生命值";
                case AttributeIds.Speed: return "速度";
                case AttributeIds.CriticalRate: return "暴击率";
                case AttributeIds.CriticalDamage: return "暴击伤害";
                default: return $"属性{attributeId}";
            }
        }

        void PrintAttributes()
        {
            Debug.Log("=== 当前属性 ===");
            Debug.Log($"攻击力: {attributes.GetCurrentValue(AttributeIds.Attack)} (基础: {attributes.GetBaseValue(AttributeIds.Attack)})");
            Debug.Log($"防御力: {attributes.GetCurrentValue(AttributeIds.Defense)} (基础: {attributes.GetBaseValue(AttributeIds.Defense)})");
            Debug.Log($"生命值: {attributes.GetCurrentValue(AttributeIds.Health)} (基础: {attributes.GetBaseValue(AttributeIds.Health)})");
            Debug.Log($"速度: {attributes.GetCurrentValue(AttributeIds.Speed)} (基础: {attributes.GetBaseValue(AttributeIds.Speed)})");
        }

        void PrintModifiers()
        {
            Debug.Log("=== 修饰器信息 ===");
            
            var attackModifiers = attributes.GetModifiers(AttributeIds.Attack);
            Debug.Log($"攻击力修饰器数量: {attackModifiers.Count}");
            foreach (var modifier in attackModifiers)
            {
                string type = modifier.Type == ModifierType.Percentage ? "百分比" : "绝对值";
                string value = modifier.Type == ModifierType.Percentage ? 
                    $"{AttributeModifier.FromPercentageValue(modifier.Value):P1}" : 
                    modifier.Value.ToString();
                Debug.Log($"  - {type}: {value} (优先级: {modifier.Priority})");
            }
        }

        // UI按钮回调方法
        [ContextMenu("移除武器")]
        public void RemoveWeapon()
        {
            if (weaponModifier != null)
            {
                attributes.RemoveModifier(AttributeIds.Attack, weaponModifier);
                weaponModifier = null;
                Debug.Log("武器已移除");
            }
        }

        [ContextMenu("移除护甲")]
        public void RemoveArmor()
        {
            if (armorModifier != null)
            {
                attributes.RemoveModifier(AttributeIds.Defense, armorModifier);
                armorModifier = null;
                Debug.Log("护甲已移除");
            }
        }

        [ContextMenu("移除狂暴")]
        public void RemoveBerserk()
        {
            if (berserkModifier != null)
            {
                attributes.RemoveModifier(AttributeIds.Attack, berserkModifier);
                berserkModifier = null;
                Debug.Log("狂暴效果已移除");
            }
        }

        [ContextMenu("移除护盾")]
        public void RemoveShield()
        {
            if (shieldModifier != null)
            {
                attributes.RemoveModifier(AttributeIds.Defense, shieldModifier);
                shieldModifier = null;
                Debug.Log("护盾效果已移除");
            }
        }

        [ContextMenu("打印属性")]
        public void PrintCurrentAttributes()
        {
            PrintAttributes();
            PrintModifiers();
        }

        [ContextMenu("测试依赖关系")]
        public void TestDependencies()
        {
            // 添加依赖关系：暴击伤害基于攻击力
            attributes.AddDependency(AttributeIds.CriticalDamage, AttributeIds.Attack);
            
            // 设置基础值
            attributes.SetBaseValue(AttributeIds.CriticalDamage, 50);
            
            // 添加基于攻击力的暴击伤害修饰器
            var criticalModifier = attributes.AddBasedModifier(
                AttributeIds.CriticalDamage, 
                AttributeIds.Attack, 
                0.1f, // 攻击力的10%
                ModifierPriority.Buff
            );
            
            Debug.Log($"暴击伤害: {attributes.GetCurrentValue(AttributeIds.CriticalDamage)}");
            Debug.Log("当攻击力改变时，暴击伤害会自动重新计算");
        }
    }
} 